package com.gitee.llj.tools.exception;

import com.gitee.llj.tools.base.ResultModel;
import com.gitee.llj.tools.base.ResultStatus;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.servlet.ModelAndView;

@ControllerAdvice
public class HandlerExceptionResolver {
	
	@ExceptionHandler({UnauthorizedException.class})
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    public ModelAndView processUnauthenticatedException(NativeWebRequest request, UnauthorizedException e) {
        ModelAndView mv = new ModelAndView();
        mv.addObject("exception", e);  
        mv.setViewName("unauthorized");  
        return mv;  
    } 

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public ResultModel resolveException(Exception ex){
    	ex.printStackTrace();
        if (ex instanceof NormalException){
            return new ResultModel(ResultStatus.FAIL.getSuccess(),ex.getMessage());
        }
        return ResultModel.error(ResultStatus.SYSTEM_ERROR);
    }

}
